package net.schastny.shopmanager.web;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

//import org.log4j.Logger;
//import org.log4j.LoggerFactory;
import net.schastny.shopmanager.domain.BaseOrder;
import net.schastny.shopmanager.domain.Contact;
import net.schastny.shopmanager.domain.Parsel;
import net.schastny.shopmanager.domain.PreOrder;
import net.schastny.shopmanager.service.BaseOrderService;
import net.schastny.shopmanager.service.ContactService;
import net.schastny.shopmanager.service.ParselService;
import net.schastny.shopmanager.service.PreOrderService;

import org.apache.log4j.Logger;
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	@Autowired
    private BaseOrderService baseorderService;
	
	@Autowired
    private PreOrderService preorderService;
	
	@Autowired
    private ParselService parselService;
	
	@Autowired
    private ContactService contactService;
	
	private static final Logger logger = Logger.getLogger(HomeController.class);
	//private static final Log logger = LogFactory.getLog(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public ModelAndView home(Locale locale, Model model) {
		System.out.println("It's index");
		
	    logger.info("Welcome home! the client locale is "+ locale.toString());
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		String name = auth.getName();
		model.addAttribute("username", name);
		
		model.addAttribute("baseOrder", new BaseOrder());
		model.addAttribute("baseOrderList", baseorderService.listBaseOrder());
        
		model.addAttribute("preorder", new PreOrder());
		model.addAttribute("preorderList", preorderService.listPreOrder());

		model.addAttribute("parsel", new Parsel());
		model.addAttribute("parselList", parselService.listParsel());

		model.addAttribute("contact", new Contact());
		model.addAttribute("contactList", contactService.listContact());
		System.out.println("It's index777777");
		
		return new ModelAndView("home", "model", model);
	}
	
	@RequestMapping("/")
    public String home() {
		System.out.println("It's index rooot");
		
        return "redirect:/index";
    }
	

	
}
